package com.oa.pm.service.impl;

import com.joneying.common.annotation.CommonService;
import com.joneying.common.web.response.Resp;
import com.oa.core.PageResult;
import com.oa.core.enumm.ProcessDefineEnum;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import com.oa.core.utils.DateUtil;
import com.oa.core.utils.ExcelUtil;
import com.oa.pm.dto.RecruitmentAddDTO;
import com.oa.pm.dto.RecruitmentAuditDTO;
import com.oa.pm.dto.RecruitmentListDTO;
import com.oa.pm.entity.Recruitment;
import com.oa.pm.mapper.RecruitmentMapper;
import com.oa.pm.service.IRecruitmentService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.oa.pm.vo.RecruitmentDetailsVO;
import com.oa.pm.vo.RecruitmentListVO;
import com.oa.system.dto.ApplyApprovalAddDTO;
import com.oa.system.dto.ApprovalDTO;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 招聘申请表 服务实现类
 * </p>
 *
 * @author syb123
 * @since 2019-01-22
 */
@Service
public class RecruitmentServiceImpl extends ServiceImpl<RecruitmentMapper, Recruitment> implements IRecruitmentService {

    private final RecruitmentMapper recruitmentMapper;

    @Autowired
    public RecruitmentServiceImpl(RecruitmentMapper recruitmentMapper) {
        this.recruitmentMapper = recruitmentMapper;
    }

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @Override
    @CommonService
    public PageResult<Recruitment> findList(Integer pageNum, Integer pageSize) {
        if(pageNum == null){
            List<Recruitment> list = recruitmentMapper.findList();
            return new PageResult<>(list);
        }else{
            // 当前页，总条数 构造 page 对象
            Page<Recruitment> page = new Page<>(pageNum, pageSize);
            //查询
            List<Recruitment> list = recruitmentMapper.findList(page);
            return new PageResult<>(list);
        }
    }

    /**
    * 根据id查询
    */
    @Override
    @CommonService
    public RecruitmentDetailsVO findRecruitmentById(String id){
        return recruitmentMapper.findRecruitmentDetails(id);
    }

    @CommonService
    @Override
    public PageResult<RecruitmentListVO> findRecruitmentList(RecruitmentListDTO recruitmentListDTO) {
        Page page = recruitmentListDTO.getPage();
        return new PageResult<>(recruitmentMapper.findRecruitmentList(page, recruitmentListDTO), page.getPages());
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp add(RecruitmentAddDTO recruitmentAddDTO) throws DataIsEmptyException, BusinessException {
        Recruitment recruitment = new Recruitment();
        BeanUtils.copyProperties(recruitmentAddDTO, recruitment);
        recruitment.setApplyTime(new Date());
        if(insert(recruitment)){
            User user = userService.selectById(recruitment.getProposer());

            ApplyApprovalAddDTO applyApprovalAddDTO = new ApplyApprovalAddDTO();
            applyApprovalAddDTO.setApplyId(recruitment.getId());
            applyApprovalAddDTO.setApprovalProcessId(ProcessDefineEnum.RECRUITMENT.getId());
            applyApprovalAddDTO.setApplyApprovalName(user.getName() + ProcessDefineEnum.RECRUITMENT.getText());
            applyApprovalAddDTO.setApprovalUserId(user.getId());
            return applyApprovalService.addApproval(applyApprovalAddDTO);

        }
        throw new DataIsEmptyException("提交申请失败");
    }

    @Transactional(rollbackFor = Exception.class)
    @CommonService
    @Override
    public Resp audit(RecruitmentAuditDTO recruitmentAuditDTO) throws DataIsEmptyException {
        ApprovalDTO approvalDTO = new ApprovalDTO();
        approvalDTO.setId(Long.valueOf(recruitmentAuditDTO.getApplyApprovalId()));
        approvalDTO.setUserId("1");
        approvalDTO.setStatus(recruitmentAuditDTO.getAuditStatus());

        return applyApprovalService.audit(approvalDTO, d -> {
            Recruitment recruitment = new Recruitment();
            BeanUtils.copyProperties(recruitmentAuditDTO, recruitment);
            updateById(recruitment);
        });
    }

    @Override
    public void export(RecruitmentListDTO recruitmentListDTO, HttpServletResponse response) {
        List<RecruitmentListVO> recruitmentListVOS;
        if(recruitmentListDTO.getPage() != null){
            Page page = new Page();
            recruitmentListVOS = recruitmentMapper.findRecruitmentList(page, recruitmentListDTO);
        }else{
            recruitmentListVOS = recruitmentMapper.findRecruitmentList(new Page(), recruitmentListDTO);
        }

        String[] rows = {"id", "提交申请时间", "部门", "申请人", "需要人数", "拟任岗位及职务", "直接主管", "增补理由", "审批节点", "审批状态", "审批状态文本"};
        try {
            ExcelUtil.downloadExcel("招聘申请列表" + DateUtil.getDayMonth(), rows, recruitmentListVOS, "招聘申请列表" + DateUtil.getDayMonth()+".xls", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
